package org.android.com;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class Setting extends Activity {
	
	private final static String MY_PREFERENCES = "MyPref";
	private final static String TEXT_DATA_KEY = "textData";
	
	int millisH = 0;
	int millisS = 0;
	int millisM = 0;
	int millis = 0;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        
        overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
        
        updatePreferencesData();
        
        SeekBar seekBarOre = (SeekBar)findViewById(R.id.SeekBarOre);
        seekBarOre.setProgress(millisH);
        SeekBar seekBarMin = (SeekBar) findViewById(R.id.SeekBarMin);
        seekBarMin.setProgress(millisM);
        SeekBar seekBarSec = (SeekBar) findViewById(R.id.SeekBarSec);
        seekBarSec.setProgress(millisS);
        final TextView seekBarValueOre = (TextView) findViewById(R.id.SeekBarOreValue);
        seekBarValueOre.setText("Hours: " + millisH);
        final TextView seekBarValueMin = (TextView) findViewById(R.id.SeekBarMinValue);
        seekBarValueMin.setText("Minuts: " + millisM);
        final TextView seekBarValueSec = (TextView) findViewById(R.id.SeekBarSecValue);
        seekBarValueSec.setText("Seconds: " + millisS);
        
        seekBarOre.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				seekBarValueOre.setText("Hour: " + String.valueOf(progress));
				millisH = progress;
			}
		});

        seekBarMin.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				seekBarValueMin.setText("Minuts: " + String.valueOf(progress));
				millisM = progress;
			}
		});
        
        seekBarSec.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				seekBarValueSec.setText("Seconds: " + String.valueOf(progress));
				millisS = progress;
				//Log.v("Log Tag", "millis: " + millisS);
			}
		});
	}
	
	public void ritorna (View view){
    	Intent i = new Intent(view.getContext(),CountDownTest_1.class);
    	//view.getContext().startActivity(i);
    	millis = ((millisH*3600)+(millisS) + (millisM*60))*1000;
    	Log.v("Log Tag", "millisR: " + millis);
    	String pkg = getPackageName();
    	i.putExtra(pkg+".myMillis",millis);
    	savePreferencesData();
    	
    	startActivity(i);
    }
	
	public void savePreferencesData() {

        SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
 
        SharedPreferences.Editor editor = prefs.edit();

        String textData = String.valueOf (millis);
        if (textData != null) {

                editor.putString(TEXT_DATA_KEY, textData.toString());
                editor.commit();
        }
        updatePreferencesData();
}
	
	private void updatePreferencesData(){
		 
        SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
        
        int textData = Integer.parseInt(prefs.getString(TEXT_DATA_KEY, "0"));
        
        millis = textData;
        
        long seconds = millis / 1000;
    	long minutes = seconds / 60;
    	long hours = minutes / 60;
    	
    	seconds = seconds % 60;
    	minutes = minutes % 60;
    	hours = hours % 60;
    	
    	String secondsD = String.valueOf(seconds);
    	String minutesD = String.valueOf(minutes);
    	String hoursD = String.valueOf(hours);
    	
    	if (seconds < 10)
    		secondsD = "0" + seconds;
    	if (minutes < 10)
    		minutesD = "0" + minutes;
    	if (hours < 10)
    		hoursD = "0" + hours;
    	
    	millisS = Integer.parseInt(secondsD);
    	millisM = Integer.parseInt(minutesD);
    	millisH = Integer.parseInt(hoursD);
    	        
}
	
	@Override
    protected void onDestroy(){
    	super.onDestroy();
    	Log.e("Log","Destroy");
    	
    }

}
